import { defineStore } from 'pinia'
import piniaPersistConfig from '../../piniaPersist'
import { carInfoDemoData } from '~/src/constant'

export const useCarStore = defineStore('car-store', {
    state: (): Car.Setting => ({
        carInfoList: carInfoDemoData,
    }),
    getters: {
        /** 根据车架号获取车辆信息 */
        getCarInfo() {
            return (frameNumber: string | number) => this.carInfoList.find(item => item.frameNumber === frameNumber) || {}
        }
    },
    actions: {
        /** 批量导入车辆信息 */
        importCarInfo(newCarInfoList: Car.CarInfo[]) {
            this.carInfoList = this.carInfoList.concat(newCarInfoList)
        },
        /** 添加单条车辆信息 */
        addCarInfo(newCarInfo: Car.CarInfo) {
            this.carInfoList.push(newCarInfo)
        },
        /** 修改单条车辆信息 */
        editCarInfo(index: number, newCarInfo: Car.CarInfo) {
            this.carInfoList[index] = newCarInfo
        },
        /** 删除单条车辆信息 */
        deleteCarInfo(index: number) {
            this.carInfoList.splice(index, 1)
        }
    },
    persist: piniaPersistConfig('car')
})
